package elitech.vietnam.myfashion.wsclient;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import org.json.JSONException;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import elitech.vietnam.myfashion.entites.Category;
import elitech.vietnam.myfashion.entites.City;
import elitech.vietnam.myfashion.entites.Color;
import elitech.vietnam.myfashion.entites.Comment;
import elitech.vietnam.myfashion.entites.District;
import elitech.vietnam.myfashion.entites.Member;
import elitech.vietnam.myfashion.entites.Notify;
import elitech.vietnam.myfashion.entites.OrderHistory;
import elitech.vietnam.myfashion.entites.Post;
import elitech.vietnam.myfashion.entites.Product;
import elitech.vietnam.myfashion.entites.ProductDetail;
import elitech.vietnam.myfashion.entites.Size;
import elitech.vietnam.myfashion.utilities.Utilities;
import elitech.vietnam.myfashion.wsclient.Helper.Param;

public class Service {

	public static final String SALT = "orchipro";

	/**
	 * Method get posts of all Styler, order by date created
	 * 
	 * @param accountID
	 *            user's ID
	 * @param start: first item position
	 * @param count: number of items to get
	 * @return null = error
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Post> getStyler(int accountID, int start, int count) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("start", start));
		params.add(new Param("count", count));

		String response;
		response = new Helper().sendRequest("GetStyler", params);

		Type type = new TypeToken<ArrayList<Post>>() {}.getType();

		ArrayList<Post> result = new Gson().fromJson(response, type);
		return result;

	}

	/**
	 * Change user's password
	 * 
	 * @param accountID
	 *            user's ID
	 * @param password
	 *            user's password
	 * @return 0: error 1: success
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int changePassword(int accountID, String password) throws IOException, XmlPullParserException {
		int result = 0;
		password = encodeString(password);
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("password", Utilities.getMD5(password + SALT)));

		String response = new Helper().sendRequest("ChangePassword", params);

		result = Integer.parseInt(response);

		return result;
	}

	/**
	 * Change user's profile (slogan)
	 * 
	 * @param accountID
	 *            : user ID
	 * @return 0: error 1: success
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int newProfile(int accountID, String profile) throws IOException, XmlPullParserException {
		profile = encodeString(profile);

		int result = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("profile", profile));

		String response = new Helper().sendRequest("NewProfile", params);
		result = Integer.parseInt(response);

		return result;
	}

	/**
	 * Get all user's styles
	 * 
	 * @param accountID
	 *            user's ID
	 * @param start: first item position
	 * @param count: number of items to get
	 * @return null = error
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Post> getMyStyle(int accountID, int start, int count) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("start", start));
		params.add(new Param("count", count));

		String response = new Helper().sendRequest("GetMyStyle", params);
		Type type = new TypeToken<ArrayList<Post>>() {}.getType();

		ArrayList<Post> result = new Gson().fromJson(response, type);

		return result;
	}

	/**
	 * Get top 100 Product order by likes with specific date
	 * 
	 * @param date
	 * @param accountID
	 * @return list product
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Product> getBestOfDay(Date date, int accountID) throws IOException, XmlPullParserException {
		String day = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(date);
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("day", day));
		params.add(new Param("account", accountID));

		String response = new Helper().sendRequest("GetBestOfDay", params);
		
		Type type = new TypeToken<ArrayList<Product>>() {}.getType();

		ArrayList<Product> result = new Gson().fromJson(response, type);
		
		return result;
	}

	/**
	 * Get list product Detail by product ID
	 * 
	 * @param productID
	 * @return list product detail
	 * @throws XmlPullParserException
	 * @throws IOException
	 */
	public ArrayList<ProductDetail> getProductDetail(int productID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("product", productID));

		String response = new Helper().sendRequest("GetProductDetail", params);
		Type type = new TypeToken<ArrayList<ProductDetail>>() {}.getType();

		ArrayList<ProductDetail> result = new Gson().fromJson(response, type);

		return result;
	}

	/**
	 * Add new order detail
	 * 
	 * @param orderID
	 * @param productID
	 * @param color
	 * @param size
	 * @param price
	 * @param quantity
	 * @return >0: id of new order detail
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int newOrderDetail(int orderID, int productID, int color, int size, int price, int quantity) throws IOException, XmlPullParserException {
		int result = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("order", orderID));
		params.add(new Param("product", productID));
		params.add(new Param("color", color));
		params.add(new Param("price", size));
		params.add(new Param("size", price));
		params.add(new Param("quantity", price));

		String response = new Helper().sendRequest("NewOrderDetail", params);
		result = Integer.parseInt(response);

		return result;
	}

	/**
	 * Add new order
	 * 
	 * @param accountID
	 * @param address
	 * @param city
	 * @param state
	 * @param phone
	 * @param payment
	 * @param shipping
	 * @param memo
	 * @param detail
	 * @return >0: id of new order
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int newOrder(int accountID, String email, String name, String address, String city, String state, String phone, int payment, int shipping, double shippingFee, String memo, String detail) throws IOException, XmlPullParserException {
		int result = 0;
		email = encodeString(email);
		name = encodeString(name);
		address = encodeString(address);
		city = encodeString(city);
		state = encodeString(state);
		phone = encodeString(phone);
		String shipFee = (shippingFee+"").replace(".0", "");
		detail = encodeString(detail);
		memo = encodeString(memo);
		
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("address", address));
		params.add(new Param("city", city));
		params.add(new Param("state", state));
		params.add(new Param("phone", phone));
		params.add(new Param("payment", payment));
		params.add(new Param("ship", shipping));
		params.add(new Param("shippingfee", shipFee));
		params.add(new Param("memo", memo));
		params.add(new Param("detail", detail));
		params.add(new Param("email", email));
		params.add(new Param("name", name));

		String response = new Helper().sendRequest("NewOrder1", params);
		result = Integer.parseInt(response);

		return result;
	}

	/**
	 * Add new comment
	 * 
	 * @param productID
	 * @param accountID
	 * @param content
	 * @param type
	 * @return >0: id of new comment
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int newComment(int productID, int accountID, String content, int type) throws IOException, XmlPullParserException {
		content = encodeString(content);
		
		int result = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("product", productID));
		params.add(new Param("account", accountID));
		params.add(new Param("content", content));
		params.add(new Param("type", type));

		String response = new Helper().sendRequest("NewComment1", params);
		result = Integer.parseInt(response);

		return result;
	}

	/**
	 * Add new like
	 * 
	 * @param accountID
	 * @param productID
	 * @param liked
	 *            : current like status
	 * @param type
	 *            1: product 2: post
	 * @return like count of product or post
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int newLike(int accountID, int productID, boolean liked, int type) throws IOException, XmlPullParserException {
		int result = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("product", productID));
		params.add(new Param("account", accountID));
		params.add(new Param("liked", (liked) ? -1 : 1));
		params.add(new Param("type", type));

		String response = new Helper().sendRequest("NewLike", params);
		result = Integer.parseInt(response);

		return result;
	}

	/**
	 * Get list comment of a product
	 * 
	 * @param productID
	 * @return null = error
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Comment> getCommentOfProduct(int productID, int type) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("product", productID));
		params.add(new Param("type", type));

		String response = new Helper().sendRequest("GetCommentOfProduct", params);
		Type type1 = new TypeToken<ArrayList<Comment>>() {}.getType();

		ArrayList<Comment> result = new Gson().fromJson(response, type1);

		return result;
	}

	/**
	 * Register new account
	 * 
	 * @param name
	 * @param email
	 * @param password
	 * @return -1: email exist 0: error 1: success
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int signUp(String name, String email, String password, String gcmId) throws IOException, XmlPullParserException {
		name = encodeString(name);
		email = encodeString(email);
		password = encodeString(password);
		gcmId = encodeString(gcmId);

		int result = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("name", name));
		params.add(new Param("email", email));
		params.add(new Param("password", Utilities.getMD5(password + SALT)));
		params.add(new Param("gcmId", gcmId));

		String response = new Helper().sendRequest("Signup1", params);
		result = Integer.parseInt(response);

		return result;
	}

	/**
	 * Log in
	 * 
	 * @param email
	 * @param password
	 * @return -1: email does not exist 0: wrong password >0: success (member
	 *         id)
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int login(String email, String password) throws IOException, XmlPullParserException {
		email = encodeString(email);
		password = encodeString(password);

		int result = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("email", email));
		params.add(new Param("password", Utilities.getMD5(password + SALT)));

		String response = new Helper().sendRequest("CheckLogin", params);
		result = Integer.parseInt(response);

		return result;
	}

	/**
	 * Get member information by email
	 * 
	 * @param email
	 * @param myID
	 * @return {@link Member}
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public Member getInfoMemberByEmail(String email, int myID) throws IOException, XmlPullParserException {
		email = encodeString(email);

		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("email", email));
		params.add(new Param("myid", myID));

		String response = new Helper().sendRequest("GetInfoMemberByEmail", params);

		Member result = new Gson().fromJson(response, Member.class);

		return result;
	}

	/**
	 * Get list order history of an account
	 * 
	 * @param accountID
	 * @return list {@link OrderHistory} of an account
	 * @throws IOException
	 * @throws XmlPullParserException
	 * @throws JSONException
	 */
	public ArrayList<OrderHistory> getOrderHistory(int accountID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));

		String response = new Helper().sendRequest("GetOrderHistory", params);
		Type type = new TypeToken<ArrayList<OrderHistory>>(){}.getType();
		
		ArrayList<OrderHistory> result = new Gson().fromJson(response, type);

		return result;
		//return OrderHistory.parseFromJSON(response);
	}

	/**
	 * Get list product size
	 * 
	 * @param productID
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Size> getProductSize(int productID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("product", productID));

		String response = new Helper().sendRequest("GetProductSize", params);

		Type type = new TypeToken<ArrayList<Size>>() {}.getType();

		ArrayList<Size> result = new Gson().fromJson(response, type);
		return result;
	}

	/**
	 * Get list product color
	 * 
	 * @param productID
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Color> getProductColor(int productID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("product", productID));

		String response = new Helper().sendRequest("GetProductColor", params);

		Type type = new TypeToken<ArrayList<Color>>() {}.getType();

		ArrayList<Color> result = new Gson().fromJson(response, type);
		return result;
	}
	
	/**
	 * Get list category
	 * 
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Category> getCategory() throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("category", ""));
		String response = new Helper().sendRequest("GetCategory", params);

		Type type = new TypeToken<ArrayList<Category>>() {}.getType();
		
		ArrayList<Category> result = new Gson().fromJson(response, type);
		return result;
	}
	
	/**
	 * Get list product of a category, order by likes, limit 100
	 * 
	 * @param productID
	 * @param accountID
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Product> getProductCategory(int categoryID, int accountID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("category", categoryID));
		params.add(new Param("account", accountID));

		String response = new Helper().sendRequest("GetProductCategory", params);
		Type type = new TypeToken<ArrayList<Product>>() {}.getType();

		ArrayList<Product> result = new Gson().fromJson(response, type);

		return result;
	}
	
	/**
	 * Post a new style
	 * 
	 * @param accountID
	 * @param title
	 * @param image
	 * @param content
	 * @return	-1: failed
	 * 			>0: success (post ID)
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int newStyle(int accountID, String title, String image, String content) throws IOException, XmlPullParserException {
		title = encodeString(title);
		image = encodeString(image);
		content = encodeString(content);
		
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("title", title));
		params.add(new Param("image", image));
		params.add(new Param("content", content));
		
		String response = new Helper().sendRequest("NewStyle", params);
		
		int result = Integer.parseInt(response);
		
		return result;
	}
	
	/**
	 * Check email exists
	 * 
	 * @param email
	 * @return	1: exists
	 * 			0: no exists
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int checkEmail(String email) throws IOException, XmlPullParserException {
		email = encodeString(email);
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("email", email));
		
		String response = new Helper().sendRequest("CheckEmail", params);
		
		int result = Integer.parseInt(response);
		
		return result;
	}
	
	/**
	 * Get info Member by ID
	 * 
	 * @param accountID
	 * @param myID
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public Member getInfoMemberByID(int accountID, int myID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("myid", myID));

		String response = new Helper().sendRequest("GetInfoMemberByID", params);

		Member result = new Gson().fromJson(response, Member.class);

		return result;
	}
	
	/**
	 * Get list city
	 * 
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<City> getListCity() throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", 1));

		String response = new Helper().sendRequest("GetListCity", params);
		Type type = new TypeToken<ArrayList<City>>() {}.getType();

		ArrayList<City> result = new Gson().fromJson(response, type);

		return result;
	}
	
	/**
	 * Get list district
	 * 
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<District> getListDistrict() throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", 1));

		String response = new Helper().sendRequest("GetListDistrict", params);
		Type type = new TypeToken<ArrayList<District>>() {}.getType();

		ArrayList<District> result = new Gson().fromJson(response, type);

		return result;
	}
	
	/**
	 * Get list favorite product
	 * @param accountID
	 * @param start
	 * @param count
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Product> getListFavorite(int accountID, int start, int count) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("start", start));
		params.add(new Param("count", count));

		String response = new Helper().sendRequest("GetListFavoriteProduct", params);
		Type type = new TypeToken<ArrayList<Product>>() {}.getType();

		ArrayList<Product> result = new Gson().fromJson(response, type);

		return result;
	}
	
	/**
	 * Edit member profile
	 * @param accountID
	 * @param name
	 * @param image
	 * @param profile
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int editProfile(int accountID, String nickname, String image, String profile) throws IOException, XmlPullParserException {
		nickname = encodeString(nickname);
		image = encodeString(image);
		profile = encodeString(profile);
		
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("nickname", nickname));
		params.add(new Param("image", image));
		params.add(new Param("profile", profile));
		
		String response = new Helper().sendRequest("EditProfile", params);
		
		int result = Integer.parseInt(response);
		
		return result;
	}
	
	/**
	 * Follow a member
	 * @param accountID
	 * @param followID
	 * @return (0, 1) = (false, true);
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int follow(int accountID, int followID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("follow", followID));
		
		String response = new Helper().sendRequest("Follow", params);
		
		int result = Integer.parseInt(response);
		
		return result;
	}
	
	/**
	 * UnFollow a member
	 * @param accountID
	 * @param followID
	 * @return (0, 1) = (false, true);
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int unFollow(int accountID, int followID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("follow", followID));
		
		String response = new Helper().sendRequest("UnFollow", params);
		
		int result = Integer.parseInt(response);
		
		return result;
	}
	
	/**
	 * Delete a follower
	 * @param accountID
	 * @param followID
	 * @return (0, 1) = (false, true);
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int deleteFollower(int accountID, int followID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("follow", followID));
		
		String response = new Helper().sendRequest("DeleteFollower", params);
		
		int result = Integer.parseInt(response);
		
		return result;
	}
	
	/**
	 * Get follower of a member
	 * @param accountID
	 * @return ArrayList<Member>
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Member> getFollower(int accountID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		
		String response = new Helper().sendRequest("GetFollower", params);
		Type type = new TypeToken<ArrayList<Member>>() {}.getType();

		ArrayList<Member> result = new Gson().fromJson(response, type);

		return result;
	}
	
	/**
	 * Get member following
	 * @param accountID
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Member> getFollowing(int accountID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		
		String response = new Helper().sendRequest("GetFollowing", params);
		Type type = new TypeToken<ArrayList<Member>>() {}.getType();

		ArrayList<Member> result = new Gson().fromJson(response, type);

		return result;
	}
	
	/**
	 * Get style of followed member
	 * @param accountID
	 * @param start
	 * @param count
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Post> getFollowStyle(int accountID, int start, int count) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("start", start));
		params.add(new Param("count", count));
		
		String response = new Helper().sendRequest("GetFollowStyle", params);
		Type type = new TypeToken<ArrayList<Post>>() {}.getType();

		ArrayList<Post> result = new Gson().fromJson(response, type);

		return result;
	}
	
	/**
	 * Delete an order
	 * @param orderID
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int deleteOrder(int orderID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("idorder", orderID));
		
		String response = new Helper().sendRequest("DeleteOrder", params);
		
		int result = Integer.parseInt(response);
		
		return result;
	}
	
	/**
	 * Get list children of a category
	 * 
	 * @param categoryID
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Category> getChildCategories(int categoryID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("category", categoryID));
		
		String response = new Helper().sendRequest("GetChildCategories", params);
		Type type = new TypeToken<ArrayList<Category>>() {}.getType();

		ArrayList<Category> result = new Gson().fromJson(response, type);

		return result;
	}
	
	/**
	 * Delete order
	 * 
	 * @param orderID
	 * @return (0, 1) = (false, true)
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int deleteOrder1(int orderID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("order", orderID));
		
		String response = new Helper().sendRequest("DeleteOrder1", params);
		
		int result = Integer.parseInt(response);
		
		return result;
	}
	
	/**
	 * Delete post
	 * 
	 * @param postID
	 * @return (0, 1) = (false, true)
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int deletePost(int postID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("post", postID));
		
		String response = new Helper().sendRequest("DeletePost", params);
		
		int result = Integer.parseInt(response);
		
		return result;
	}
	
	/**
	 * Update member's Google Cloud Message ID
	 * 
	 * @param memID
	 * @param gcmID
	 * @return (0, 1) = (false, true)
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int updateGCMID(int memID, String gcmID) throws IOException, XmlPullParserException {
		gcmID = encodeString(gcmID);
		
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("idmem", memID));
		params.add(new Param("gcmid", gcmID));
		
		String response = new Helper().sendRequest("UpdateGCMID", params);
		
		int result = Integer.parseInt(response);
		
		return result;
	}
	
	/**
	 * Send Comment Notifications
	 * 
	 * @param commentID
	 * @param type
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public void notifyComment(int commentID, int type) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("comment", commentID));
		params.add(new Param("type", type));
		
		new Helper().sendRequest("NotifyComment", params);
	}
	
	/**
	 * Get product info by ID
	 * 
	 * @param productID
	 * @param accountID
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public Product getProductByID(int productID, int accountID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("product", productID));
		params.add(new Param("account", accountID));
		

		String response = new Helper().sendRequest("GetProductByID", params);

		Product result = new Gson().fromJson(response, Product.class);

		return result;
	}
	
	/**
	 * Get Post info by ID
	 * 
	 * @param styleID
	 * @param accountID
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public Post getStyleByID(int styleID, int accountID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("style", styleID));
		params.add(new Param("account", accountID));
		

		String response = new Helper().sendRequest("GetStyleByID", params);

		Post result = new Gson().fromJson(response, Post.class);

		return result;
	}
	
	/**
	 * Perform click onto a Product
	 * 
	 * @param productID
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public void viewProduct(int productID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("product", productID));
		
		new Helper().sendRequest("ProducClicked", params);
	}
	
	/**
	 * Get recommended products of a product
	 * 
	 * @param productID
	 * @param accountID
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Product> getProductRecommended(int productID, int accountID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("product", productID));
		params.add(new Param("account", accountID));
		
		String response = new Helper().sendRequest("GetProductRecommended", params);
		Type type = new TypeToken<ArrayList<Product>>() {}.getType();
		Log.w("Shopping","getProductRecommended: "+response);
		ArrayList<Product> result = new Gson().fromJson(response, type);

		return result;
	}
	
	/**
	 * Forgot Password
	 * 
	 * @param email
	 * @return (0, 1) = (failed, success);
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int forgotPassword(String email) throws IOException, XmlPullParserException {
		email = encodeString(email);

		int result = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("email", email));
		
		String response = new Helper().sendRequest("ForgotPassword", params);
		result = Integer.parseInt(response);

		return result;
	}
	
	/**
	 * Update shipping address of current member
	 * 
	 * @param idmem
	 * @param address
	 * @param city
	 * @param district
	 * @param phone
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int updateMemberShippingInfo(int idmem, String address, int city, int district, String phone) throws IOException, XmlPullParserException {
		address = encodeString(address);
		phone = encodeString(phone);

		int result = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("idmem", idmem));
		params.add(new Param("address", address));
		params.add(new Param("city", city));
		params.add(new Param("district", district));
		params.add(new Param("phone", phone));
		
		String response = new Helper().sendRequest("UpdateMemberShippingInfo", params);
		result = Integer.parseInt(response);

		return result;
	}
	
	/**
	 * Get style of member
	 * @param accountID
	 * @param memberID
	 * @param start
	 * @param count
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Post> getMemberStyle(int accountID, int memberID, int start, int count) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("account", accountID));
		params.add(new Param("member", memberID));
		params.add(new Param("start", start));
		params.add(new Param("count", count));
		
		String response = new Helper().sendRequest("GetMemberStyle", params);
		Type type = new TypeToken<ArrayList<Post>>() {}.getType();
		ArrayList<Post> result = new Gson().fromJson(response, type);

		return result;
	}
	
	/**
	 * Store device ID of this device in the cloud
	 * @param deviceID
	 * @param gcmID
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int newDeviceID(String deviceID, String gcmID) throws IOException, XmlPullParserException {
		deviceID = encodeString(deviceID);
		gcmID = encodeString(gcmID);

		int result = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("device", deviceID));
		params.add(new Param("gcm", gcmID));
		
		String response = new Helper().sendRequest("NewDevideID", params);
		result = Integer.parseInt(response);

		return result;
	}
	
	/**
	 * Track the app when launch
	 * @param deviceID
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public int appOpenTrack(String deviceID, String version, int api, String user, String date) throws IOException, XmlPullParserException {
		deviceID = encodeString(deviceID);

		int result = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("device", deviceID));
		params.add(new Param("version", version));
		params.add(new Param("api", api));
		params.add(new Param("user", user));
		params.add(new Param("date", date));
		
		String response = new Helper().sendRequest("AppOpenTrack2", params);
		result = Integer.parseInt(response);

		return result;
	}
	
	/**
	 * Get product of a category by gender
	 * @param categoryID
	 * @param accountID
	 * @param gender
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Product> getProductCategoryByGender(int categoryID, int accountID, int gender) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("category", categoryID));
		params.add(new Param("account", accountID));
		params.add(new Param("gender", gender));

		String response = new Helper().sendRequest("GetProductCategoryByGender", params);
		Type type = new TypeToken<ArrayList<Product>>() {}.getType();

		ArrayList<Product> result = new Gson().fromJson(response, type);

		return result;
	}
	
	/**
	 * Send notification when a friend upload new post
	 * @param postID
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public void notifyFriendPost(int postID) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("postID", postID));
		
		new Helper().sendRequest("NotifyFriendPost", params);
	}
	
	/**
	 * Send notification when a member follow 
	 * @param memId
	 * @param followId
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public void notifyFriendAdd(int memId, int followId) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("memId", memId));
		params.add(new Param("followId", followId));
		
		new Helper().sendRequest("NotifyFriendAdd", params);
	}
	
	/**
	 * Add notice
	 * @param productId
	 * @param memId
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public void addNotice(int productId, int memId) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("productId", productId));
		params.add(new Param("memId", memId));
		
		new Helper().sendRequest("AddNotice", params);
	}
	
	/**
	 * Get list notifications of user
	 * @param accountId
	 * @param start
	 * @param count
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Notify> getNotifications(int accountId, int start, int count) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("accountId", accountId));
		params.add(new Param("start", start));
		params.add(new Param("count", count));

		String response = new Helper().sendRequest("GetNotifications", params);
		Type type = new TypeToken<ArrayList<Notify>>() {}.getType();

		ArrayList<Notify> result = new Gson().fromJson(response, type);

		return result;
	}
	
	/**
	 * Set notification read
	 * @param notiId
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public void readNotification(int notiId) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("notiId", notiId));
		
		new Helper().sendRequest("ReadNotification", params);
	}
	
	/**
	 * Search product by name
	 * @param name
	 * @param accountId
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public ArrayList<Product> searchProduct(String name, int accountId) throws IOException, XmlPullParserException {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("name", encodeString(name)));
		params.add(new Param("accountId", accountId));
		
		String response = new Helper().sendRequest("SearchProduct", params);
		Type type = new TypeToken<ArrayList<Product>>() {}.getType();

		ArrayList<Product> result = new Gson().fromJson(response, type);

		return result;
	}
	
	
	
	

	private String encodeString(String string) {
		try {
			string = URLEncoder.encode(string, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return string;
	}

}
